﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ERP_RORZE_SYSTEM;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class panel_StampPrint : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;

        public panel_StampPrint()
        {
            InitializeComponent();            

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            LoadSerial();
        }
        
        private void LoadSerial()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT [SerialNo] FROM [231_RobotOrderDetail]"
                    + " WHERE ([SerialNo] IS NOT NULL) AND"
                    + " (LEN([SerialNo]) <= 8) AND"
                    + " ((FinishedDate > DateAdd(dd, 0, DateDiff(dd, 0, GETDATE())) - 500) OR (FinishedDate IS NULL))"
                    + " ORDER BY [SerialNo]";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["SerialNo"].ToString();
                    item.Value = dataReader["SerialNo"].ToString();
                    this.serialNoFrom.Properties.Items.Add(item);
                    this.serialNoTo.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot get SerialNo list");
            }
        } 

        private void buttonView_Click(object sender, EventArgs e)
        {
            if ((this.serialNoFrom.SelectedIndex < 0) || (this.serialNoTo.SelectedIndex < 0))
            {
                MessageBox.Show("Please select Serial to view report");
            }
            else
            {
                ComboboxItem item_from = (ComboboxItem)serialNoFrom.SelectedItem;
                ComboboxItem item_to = (ComboboxItem)serialNoTo.SelectedItem;
                bool comp = this.checkComp.Checked;
                reportDocument.DocumentSource = null;
                rpt_StampPrint StampPrint = new rpt_StampPrint();
                StampPrint.serialno_from = item_from.Value.ToString();
                StampPrint.serialno_to = item_to.Value.ToString();
                StampPrint.comp = comp;
                StampPrint.LoadData();
                StampPrint.CreateDocument();
                reportDocument.DocumentSource = StampPrint;
            }  
        }

        private void serialNoFrom_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.serialNoTo.SelectedIndex = this.serialNoFrom.SelectedIndex;
        }
    }
}
